package com.offcn.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.offcn.mapper.MemberMapper;
import com.offcn.mapper.SetmealMapper;
import com.offcn.pojo.Member;
import com.offcn.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: wy
 * @Date: 2022/9/3 15:51
 */
@Service
@org.springframework.stereotype.Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //在这里 给前台数据 返回 月份 和 数据
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        //声明一个 月份
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {

            //获取每一个月的最后一天
            Calendar instance = Calendar.getInstance();
            /*instance.set(Calendar.YEAR,2022);
            instance.set(Calendar.MONTH,8);
            instance.set(Calendar.DATE,0);
            Date time = instance.getTime();
            */
            try {
                instance.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(month + "-0"));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            Date time = instance.getTime();

            String format = new SimpleDateFormat("yyyy-MM-dd").format(time);


            //获取集合中每个月的会员注册量
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();

            //给月份拼接一个当月最多天数，查询该月之前所有的会员注册量

            //查询该月之前所有的会员注册量
            queryWrapper.le("regTime", format);
            Integer selectCount = memberMapper.selectCount(queryWrapper);
            memberCount.add(selectCount);
        }
        return memberCount;
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        HashMap<String, Object> map = new HashMap<>();
             /*
        这个查出来的 数据为 键值对
         */
        List<Map<String, Object>> setmealCount = setmealMapper.findSetmealCount();
        //把数据 存放到 map集合中

        return setmealCount;
    }

    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.YEAR, 2021);
        instance.set(Calendar.MONTH, 10);
        instance.set(Calendar.DATE, 0);
        Date time = instance.getTime();

        String format = new SimpleDateFormat("yyyy-MM-dd").format(time);
        System.out.println(format);
    }
}
